package com.mazaiting.classpath;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * 正常以 .class 结尾的文件读取方式
 * @author mazaiting
 * @since 2021/8/10
 */
@Slf4j
@Data
public class DirEntry implements Entry {

    /**
     * 绝对路径
     */
    private String absDir;

    public DirEntry(String path) {
        this.absDir = FileUtil.getAbsolutePath(path);
    }

    @Override
    public ClassResponse readClass(String className) {
        // 获取路径
        File file = FileUtil.file(this.absDir, className);
        ClassResponse classResponse = new ClassResponse();
        classResponse.setEntry(this);
        try {
            // 读文件
            byte[] bytes = FileUtil.readBytes(file);
            classResponse.setBytes(bytes);
        }catch (IORuntimeException e) {
            log.error("{}, 读取文件异常, {}", this.getClass().getSimpleName(), e.getMessage());
            classResponse.setError(e.getMessage());
        }
        return classResponse;
    }

    @Override
    public String toString() {
        return this.absDir;
    }
}


















